
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0,
    defaultImage:'/styles/image/useravatar.png',
    userevaluation: [],
    textVal: '',
    //默认评论条数
    commentnumbers:0,
    //默认喜欢点击数量
    // touch:0,
    // mymark:-1,
    // test:0

  },
  QueryParams: { openid: '', comment_text: '', nickName: '', avatarUrl: '', goods_id: ''},
  QueryParamslist:{goods_id:''},
  QueryParamsnumber: { goods_id: '' },
  QueryParamslike: { comment_id: '' },
  onLoad(options) {
    let goods_id=options.goods_id;
    this.QueryParamslist.goods_id = goods_id;
    this.QueryParams.goods_id = goods_id;
    this.QueryParamsnumber.goods_id = goods_id;
    this.getcommonentlist();
    this.getcommentnumbers();
    
  },
  onShow(){
    let comment_id = wx.getStorageSync('comment_id')
    wx.setStorageSync('comment_id', comment_id)

    this.getcommonentlist();
    this.getcommentnumbers();
  },
  onPullDownRefresh: function () {
    let comment_id = wx.getStorageSync('comment_id')
    wx.setStorageSync('comment_id', comment_id)
    this.getcommonentlist();
    this.getcommentnumbers();
  },

  getcommonentlist() {
    // this.QueryParamslist.goods_id = 2;
    wx.request({
      url: 'http://192.168.43.159:8080/wx/commonentlist',
      data: this.QueryParamslist,
      success: (result) => {
        this.setData({
          userevaluation: result.data.message
        })
      }
    })
   

  },
  //获取输入框信息
  handleTextInput(e) {
    console.log(e.detail.value);
    this.setData({
      textVal: e.detail.value
    })
  },
  // 提交用户评论
  handSubmit() {
    let textVal = this.data.textVal;
    const userInfo = wx.getStorageSync('userinfo');
    console.log(userInfo);
    const openid = wx.getStorageSync('OPENID');
    this.QueryParams.openid = openid;
    this.QueryParams.nickName = userInfo.nickName;
    this.QueryParams.comment_text = textVal;
    this.QueryParams.avatarUrl = userInfo.avatarUrl;
    // this.QueryParams.goods_id =2; 
    console.log(this.QueryParams);
    if (!userInfo){
       wx.showToast({
        title: '请先登陆',
        duration: 1000,
        icon: 'none',
        success: function () {
          setTimeout(function () {
            wx.navigateTo({      
              url: '../login/index',
            })
          }, 1000)
        }
      })      
      return; 
    }
    if (!textVal.trim()) {
      return;
    }

    
    wx.request({
      url: 'http://192.168.43.159:8080/wx/commonentadd',
      data:this.QueryParams,
    })
    this.setData({
      textVal: ''
    })
    this.showCommitSuccessToast();
    this.getcommonentlist();
    this.getcommentnumbers();
    
   
  },
  //评论成功
  showCommitSuccessToast: function () {
    wx.showToast({
      title: "评论成功",
      duration: 1000,
      icon: "success"
    })
  },
  //评论数量
  getcommentnumbers(){
    wx.request({
      url: 'http://192.168.43.159:8080/wx/commonentnumbers',
      data:this.QueryParamsnumber,
      success: (result) => {
        // console.log(result.data.message);
        this.setData({
          commentnumbers: result.data.message
        })
      }
    })

  },
  //点击like  如果点击就喜欢增加一个，再次点击没有作用
  handlike(e){//默认toch=1
    let index = e.currentTarget.dataset.index;

    let comment_id = this.data.userevaluation[index].comment_id;
    let mycomment_id = wx.getStorageSync('comment_id')
    this.QueryParamslike.comment_id = comment_id;
    // console.log(this.data.userevaluation[index]);
    if (comment_id === mycomment_id){
      wx.showToast({
        title: '点赞收到啦~',
        icon:'none'
      })
    }
    else{
      wx.request({
        url: 'http://192.168.43.159:8080/wx/commonentlikenumber',
        data: this.QueryParamslike,
        success: (result) => {
          wx.setStorageSync('comment_id', comment_id)
          this.getcommonentlist();

        }
      })
    }



  },


})